# coding: utf-8
from functools import wraps
from django.shortcuts import redirect
from django.utils.decorators import available_attrs
from django.core.urlresolvers import reverse
from utils import PEDIDO_SESSION_KEY


def pedido_required(view_func):
    @wraps(view_func, assigned=available_attrs(view_func))
    def _wrapped_view(request, *args, **kwargs):
        pedido = request.session[PEDIDO_SESSION_KEY]
        if pedido.usuario != request.user:
            return redirect(reverse('fazer_pedido'))
        else:
            return view_func(request, *args, **kwargs)
    
    return _wrapped_view
